<?php
/**
 * Created by PhpStorm.
 * User: refar
 * Date: 21-07-21
 * Time: 上午8:55
 */

namespace app\v2\controller;

use think\Db;
use think\Exception;
use think\Log;

class History extends Common {
    /**
     * showdoc
     * @catalog 接口文档-V2/历史记录
     * @title 以样机编号查询历史记录接口
     * @description 以样机编号查询历史记录接口
     * @method get
     * @param fixed_no 必选 int 样机编号
     * @return {"status":0,"msg":"[History][getHistoryById] success","data":{"total":1,"rows":[{"id":69827,"user_name":"劉家良","start_date":"2021-04-14 15:03:24","end_date":null,"predict_date":"2022-05-06 00:00:00","actual_user_name":null,"remark":"","instore_proof":"e7cda482d2e2b2c5849d82cdfa369b2e","assign_proof":"e7cda482d2e2b2c5849d82cdfa369b2e"}]}}
     * @url http://domain/ems-api/v2/History/getHistoryById
     * @return_param ID string 条目编号
     * @return_param user_name string 用户名
     * @return_param start_date date 开始使用时间
     * @return_param end_date date 结束使用时间
     * @return_param predict_date date 预估归还时间
     * @return_param remark string 备注
     * @return_param instore_proof string 入库凭证
     * @return_param assign_proof string 签收凭证
     * @return_param showUpdate boolean 显示管理员更新按钮
     * @remark 超过5条显示下拉框
     */
    public function getHistoryById() {
        // param
//        $offset = $this->request->param('offset'); // 前端传过来的值为(pageNo-1)*limit
//        $limit = $this->request->param('limit');
        $fixed_no = $this->request->param('fixed_no');

        try {
            $res = Db::table('sms_borrow_history')->where('FIXED_NO', $fixed_no)
                ->field('id, user_name, start_date, end_date, predict_date, actual_user_name, remark, instore_proof, assign_proof')->order('start_date')
                ->select();

            $count = Db::table('sms_borrow_history')->where('FIXED_NO', $fixed_no)->count();

            $jsonResult['total'] = $count;
            $jsonResult['rows'] = $res;

            $jsonResult['showUpdate'] = false;
            // 管理员显示编辑
            if ($this->showVisible) {
                $jsonResult['showUpdate'] = true;
            }

            return apiResponse(SUCCESS, '[History][getHistoryById] success', $jsonResult);
        } catch (Exception $e) {
            Log::record('[History][getHistoryById] error' . $e->getMessage());
            return apiResponse(ERROR, 'server error');
        }

    }
    /**
     * showdoc
     * @catalog 接口文档-V2/历史记录
     * @title 以ID更新历史记录接口
     * @description 以ID更新历史记录接口
     * @method get
     * @param id 必选 int 记录条目编号
     * @param instore_proof_id 非必要 string 签收凭证附件id
     * @param assign_proof_id 非必要 string 入库凭证附件id
     * @return {"status":0,"msg":"[History][updateHistoryAttachmentById] success","data":{"total":1,"rows":[{"id":69827, "user_name":"芜湖","start_date":"2021-07-22 13:31:06","end_date":null,"predict_date":"2021-07-11 00:00:00","remark":null}]}}
     * @url http://domain/ems-api/v2/History/updateHistoryAttachmentById
     * @remark
     */
    public function updateHistoryAttachmentById() {
        $id = $this->request->param('id');
        $instore_proof_id = $this->request->param('instore_proof_id');
        $assign_proof_id = $this->request->param('assign_proof_id');

        $data = [
            'instore_proof'=>$instore_proof_id,
            'assign_proof'=>$assign_proof_id
        ];
        try {
            Db::table('sms_borrow_history')->where('ID', $id)->update($data);

            return apiResponse(SUCCESS, '[History][updateHistoryAttachmentById] success');

        }catch (Exception $e) {
            Log::record('[History][updateHistoryAttachmentById] error ' . $e->getMessage());
            return apiResponse(ERROR, 'server error');
        }

    }

}
